home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.92 / crossfir / crossfire-0.92.5 / common / player.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  5KB  |  206 lines

  1. /*
  2.  * static char *rcsid_player_c =
  3.  *   "$Id: player.c,v 1.26 1996/03/04 09:23:18 master Exp $";
  4.  */
  5.  
  6. /*
  7.     CrossFire, A Multiplayer game for X-windows
  8.  
  9.     Copyright (C) 1994 Mark Wedel
  10.     Copyright (C) 1992 Frank Tore Johansen
  11.  
  12.     This program is free software; you can redistribute it and/or modify
  13.     it under the terms of the GNU General Public License as published by
  14.     the Free Software Foundation; either version 2 of the License, or
  15.     (at your option) any later version.
  16.  
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU General Public License
  23.     along with this program; if not, write to the Free Software
  24.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26.     The author can be reached via e-mail to master@rahul.net
  27. */
  28.  
  29. #include <global.h>
  30. #include <funcpoint.h>
  31.  
  32. /*
  33.  * Returns a newly allocated and initialised and correctly
  34.  * linked player structure.
  35.  */
  36.  
  37. player *get_player_ob() {
  38.   int i;
  39.   player *new,*tmp;
  40.  
  41.   new = (player *) CALLOC(1,sizeof(player));
  42.   if(new==NULL)
  43.     fatal(OUT_OF_MEMORY);
  44.   if(!editor) {
  45.     tmp=first_player;
  46.     while(tmp!=NULL&&tmp->next!=NULL)
  47.       tmp=tmp->next;
  48.     if(tmp!=NULL)
  49.       tmp->next=new;
  50.     else
  51.       first_player=new;
  52.   }
  53.   new->next=NULL;
  54.   new->menu=NULL;
  55.   new->sync = synchronize;
  56.   new->cur_sync = 1;
  57.   new->known_spell = 0;
  58.   new->removed=0;
  59. #ifdef SIMPLE_PARTY_SYSTEM
  60.   new->party_number=-1;
  61. #endif
  62.   new->last_known_spell = 0;
  63.   new->infoline=0;
  64.   new->infopos=0;
  65.   new->infofull=0;
  66.   new->keyboard_flush=0;
  67.   new->show_inv_icon=0;
  68.   new->infolines=INFOLINES;
  69.   new->infochars=INFOCHARS;
  70.   new->gc_root=NULL;
  71.   new->gc_xpm_floor=NULL;
  72.   new->gc_xpm_object=NULL;
  73.   new->info=(char **) malloc(sizeof(char *) * new->infolines);
  74.   for(i=0;i<new->infolines;i++) {
  75.     new->info[i]=(char *) malloc(sizeof(char) * (new->infochars + 1));
  76.     strcpy(new->info[i]," ");
  77.   }
  78.   for(i=0;i<MAX_LOOK_SIZE;i++) {
  79.     new->look_name[i]=NULL;
  80.     new->look_face[i]=blank_face->number;
  81.   }
  82.   for(i=0;i<MAX_INV_SIZE;i++) {
  83.     new->inv_name[i]=NULL;
  84.     new->inv_face[i]=blank_face->number;
  85.   }
  86.   new->commandkey =(KeyCode)('\'');
  87.   new->firekey[0] =0;
  88.   new->firekey[1] =0;
  89.   new->runkey[0] =0;
  90.   new->runkey[1] =0;
  91.   for(i=0;i<COMMAND_HASH_SIZE;i++)
  92.     new->keys[i] =NULL;
  93.   new->key_state=0;
  94.   new->key_down=0;
  95.   new->state=0;
  96.  
  97.   new->use_pixmaps = 0;
  98.   new->pixmaps = NULL;
  99.   new->masks = NULL;
  100.   new->use_pixmaps=0;
  101.   new->color_pixmaps=0;
  102.   new->ob = get_object();
  103.   new->ob->name = add_string("logon");
  104.   new->ob->type = PLAYER;
  105.   new->ob->contr = new;
  106.   new->show_what = show_all;
  107.   new->name = NULL;
  108.   new->username = NULL;
  109.   new->shoottype = range_none;
  110.   new->last_shoot = range_size;
  111.   new->no_echo =0;
  112.   new->split_window =0;
  113.   new->braced =0;
  114.   new->search_str[0]=0;
  115. #if 0
  116.   memset(new->mapdelx, 0, 32);
  117.   memset(new->mapdely, 0, 32);
  118. #endif
  119.  
  120. #ifdef SAVE_WINDOW_POSITIONS
  121.   new->valid_save_positions =0;
  122.   memset(new->win_pos, 0, 6*sizeof(save_win_pos));
  123. #endif
  124. #ifdef USE_SWAP_STATS
  125.   new->Swap_First = -1;
  126. #endif
  127. #ifdef SOUND_EFFECTS
  128.   new->play_count=0;
  129.   new->rplay_fd=0;
  130. #endif
  131.  
  132.   for (i=0; i<NUM_OUTPUT_BUFS; i++) {
  133.     new->outputs[i].buf=NULL;
  134.     new->outputs[i].first_update=0;
  135.     new->outputs[i].count=0;
  136.   }
  137.   new->outputs_sync=16;        /* Every 2 seconds */
  138.   new->outputs_count=1;        /* Keeps present behaviour */
  139.   return new;
  140. }
  141.  
  142. void free_player(player *pl) {
  143.   int i;
  144.   Key_s *key, *nextkey;
  145.  
  146.   if(first_player!=pl) {
  147.     player *prev=first_player;
  148.     while(prev!=NULL&&prev->next!=NULL&&prev->next!=pl)
  149.       prev=prev->next;
  150.     if(prev->next!=pl) {
  151.       LOG(llevError,"Free_player: Can't find previous player.\n");
  152.       exit(1);
  153.     }
  154.     prev->next=pl->next;
  155.   } else first_player=pl->next;
  156.   if(pl->ob != NULL)
  157.     free_object(pl->ob);
  158.   if(pl->name!=NULL)
  159.     free_string(pl->name);
  160.   if(pl->username != NULL)
  161.     free(pl->username);
  162.   if(pl->info) {    
  163.     for(i=0;i<pl->infolines;i++) 
  164.       free(pl->info[i]);
  165.     free(pl->info);
  166.   }
  167.   /*
  168.    * Binded keys
  169.    */
  170.   for(i=0;i<COMMAND_HASH_SIZE;i++) {
  171.     for (key=pl->keys[i]; key; key=nextkey) {
  172.     nextkey = key->next;
  173.     free(key);
  174.     }
  175.     pl->keys[i] = NULL;
  176.   }
  177.  
  178.   /* The XPM manual says that XFreePixmap and XFreeColors should be called
  179.    * when those resources are no longer needed (this applies to the 
  180.    * color pixmap (XPM) code.  Hopefully, the X11 server does so when 
  181.    * the connection is closed.  However, the connection to close the display
  182.    * is called before it gets here, so we can not do XFreePixmap or
  183.    * XFreeColors here.   Mark Wedel (master@rahul.net)
  184.    */
  185.   if(pl->pixmaps != NULL) {
  186.     free(pl->pixmaps);
  187.   }
  188.   if (pl->masks!=NULL)
  189.     free(pl->masks);
  190.  
  191. /* From my understanding of the code, this will remove the pl->menu->inv
  192.  (and all objects ->below) as well as the pl->menu object itself.  If this
  193.   isn't true, then this will need to be altered - Mark Wedel 
  194.   (master@rahul.net)
  195. */
  196.   if (pl->menu)
  197.     free_object(pl->menu);
  198.  
  199.     for (i=0; i<MAX_INV_SIZE; i++)
  200.     if (pl->inv_name[i]) free_string(pl->inv_name[i]);
  201.     for (i=0; i<MAX_LOOK_SIZE; i++)
  202.     if (pl->look_name[i]) free_string(pl->look_name[i]);
  203.  
  204.   CFREE(pl);
  205. }
  206.